package org.threebitsoftware.tbb.Contact_GUI;
import javax.swing.JOptionPane;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

import com.swtdesigner.SWTResourceManager;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Control;
import org.threebitsoftware.tbb.engine.EngineUtil;

public class FileImport_GUI extends Dialog {
	private Text pathText_;
	protected Object result_;
	protected Shell shell;
	private Button btnBrowse;
	private Button btnImport;
	private Button btnCancel;

	/**
	 * Create the shell.
	 * 
	 * @param display
	 */
	public FileImport_GUI(Shell parent, int style) {
		super(parent, style);
		setText("SWT Dialog");
	}

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
			
		Display display = getParent().getDisplay();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result_;
	}
	
	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM
				| SWT.APPLICATION_MODAL);
		shell.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		shell.setSize(438, 92);
		shell.setText("TBB File Import");
		
		// Disable the check that prevents subclassing of SWT components
		
		/**
		 * handle the file path label
		 */
		{
			Label lblPath =  new Label(shell, SWT.NONE);
			lblPath.setBounds(10, 10, 61, 18);
			lblPath.setText("File Path");
		}
		
		/**
		 * set the path textbox on the gui
		 */
		{
			pathText_ = new Text(shell, SWT.BORDER);
			pathText_.setBounds(77, 7, 259, 19);
		}

		/**
		 * Browse button function
		 */	
		{
			btnBrowse = new Button(shell, SWT.NONE);
			btnBrowse.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseDown(MouseEvent e) {					
					/**
					 * opens the dialog window and sets whatever is selected in
					 * the path text box
					 */
					FileDialog openDialog = new FileDialog(new Shell(),SWT.OPEN);
					openDialog.setFilterExtensions(new String[] {"*.txt","*.vcf"});
					openDialog.open();
					pathText_.setText(openDialog.getFilterPath() != "" ? 
							openDialog.getFilterPath()+ "\\" + 
							openDialog.getFileName() : "");
				}
			});
			btnBrowse.setBounds(342, 5, 68, 23);
			btnBrowse.setText("&Browse");
		}
		
		/**
		 * Cancel button function
		 */
		{
			btnCancel = new Button(shell, SWT.NONE);
			btnCancel.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					System.out.println("Closing the File Import dialog...");
					shell.close();
				}
			});
		
			btnCancel.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseDown(MouseEvent e) {
					shell.close();
				}
			});
			btnCancel.setBounds(206, 32, 68, 23);
			btnCancel.setText("&Cancel");
		}
		
		{
			/**
			 * handle the Import button
			 */
			btnImport = new Button(shell, SWT.NONE);
			btnImport.addMouseListener(new MouseAdapter() {
				
				/**
				 * handle the import button
				 */
				@Override
				public void mouseDown(MouseEvent e) {
					String fileExtenson = pathText_.getText();
					if(fileExtenson.substring(fileExtenson.indexOf(".")).equalsIgnoreCase(".txt")) {
						System.out.println("the simple text import should be called here");
						EngineUtil.useFileImportHandler("org.threebitsoftware.tbb.module.simpletext.SimpleTextImporter", fileExtenson);
						JOptionPane.showMessageDialog(null,"Simple Text Import finished. Check " + pathText_.getText(),
								null, 
								JOptionPane.NO_OPTION);
					} else {
						System.out.println("the vcard import should be called here");
						EngineUtil.useFileImportHandler("package org.threebitsoftware.tbb.module.vcard.VCardImporter", fileExtenson);
						JOptionPane.showMessageDialog(null,"VCard Import finished. Check " + pathText_.getText(),
								null, 
								JOptionPane.NO_OPTION);
					}
				}
			});
			btnImport.setBounds(132, 32, 68, 23);
			btnImport.setText("&Import");
		}
		shell.setTabList(new Control[]{pathText_, btnBrowse, btnImport, btnCancel});
	}
}
